package cn.tangwei.pet.service.impl;

import cn.tangwei.basic.service.impl.BaseServiceImpl;
import cn.tangwei.pet.domain.Pet;
import cn.tangwei.pet.domain.PetDetail;
import cn.tangwei.pet.mapper.PetDetailMapper;
import cn.tangwei.pet.mapper.PetMapper;
import cn.tangwei.pet.service.IPetService;
import cn.tangwei.user.domain.User;
import cn.tangwei.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PetServiceImpl  extends BaseServiceImpl<Pet> implements IPetService {

    @Autowired
    private PetMapper petMapper;
    @Autowired
    private PetDetailMapper petDetailMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public void add(Pet pet) {
        // 保存宠物信息
        petMapper.save(pet);
        // 保存宠物详情
        PetDetail detail = pet.getDetail();
        if(detail != null){
            detail.setPet_id(pet.getId());
            petDetailMapper.save(detail);
        }
    }

    @Override
    public void onsale(List<Long> ids) {
        Map<String, Object> map = new HashMap<>();
        map.put("ids", ids);
        map.put("onsaleTime", new Date());
        petMapper.onsale(map);
    }

    @Override
    public void offsale(List<Long> ids) {
        Map<String, Object> map = new HashMap<>();
        map.put("ids", ids);
        map.put("offsaleTime", new Date());
        petMapper.offSale(map);
    }

    @Override
    public void adoptPet(Long id, Long id1) {
        Pet pet = petMapper.queryById(id );
        User user = userMapper.loadByLoginInfoId(id1);
        pet.setUser_id(user.getId());
        pet.setState(0);
        pet.setOffsaletime(new Date());
        petMapper.update(pet);
    }
}
